// 验证手机号
function phoneVertify(num){
	let phoneReg = /^1(3|4|5|7|8|9)\d{9}$/
	if(!phoneReg.test(num)){
		return {
			type:true,
			error:'手机号格式不对'
		}
	}else{
		return {
			type:false,
		}
	}
}
function codeNumber(num){
	let numReg = /^\d{0,18}$/
	if(numReg.test(num)){
		return{
			type:false,
			error:' '
		}
	}
}
// 验证密码
function pasVertify(num){
	let pasReg = /^[a-zA-Z\d~\!@#\$%\^&\*\(\)_\-\+=\{\[\}\]\|\\:;\"\'\<,\>\.\?\/]{6,12}$/
	if(pasReg.test(num)){
		return {
			type:true,
		}
	}else{
		return {
			type:false,
			error:'请输入正确的密码'
		}
	}
}
// 封装请求
const path = 'http://192.168.20.128:8083/'
function request(src, method, data, header = {}){
	
	uni.showLoading({
		title: '加载中' // 数据请求前loading
	});
	
	let userInfo = uni.getStorageSync("userInfo");
	let AuthorizationStr ="";
	if(userInfo && userInfo != undefined && userInfo.id){
		AuthorizationStr += "userId_" + userInfo.id;
	}

	return new Promise((resolve, reject) => {
		uni.request({
			url: path + src,   //具体的接口地址
			method: method,
			data: data,
			header: {
				'content-type': 'application/json',
				'Authorization' : AuthorizationStr
			},
			success: function (res) {
				//判断当前用户是否被拉黑，如果被拉黑调回登录页面
				if(res.data.code == -1){
					uni.showModal({
						title: '提示',
						showCancel:false,
						content: '您已被拉黑，请联系管理员并重新登录',
						success (res) {
							if (res.confirm) {
								wx.reLaunch({
									url: '../login/main?type=blacklist'
								})
							}
						}
					})
				}
				uni.hideLoading();
				resolve(res.data);
			},
			fail: function (res) {
				uni.hideLoading();
				resolve(res.data);
			},
			complete: function (res) {
				uni.hideLoading();
				resolve(res.data);
			}
		})
	})
}

// 时间转换
function timer(time){
	var now = new Date(time);
	var	y = now.getFullYear();
	var	m = now.getMonth() + 1;
	var	d = now.getDate();
		 return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + ' ' + now.toTimeString().substr(0,8)
}

module.exports = {
	phoneVertify:phoneVertify,
	pasVertify:pasVertify,
	request:request,
	timer: timer,
	codeNumber:codeNumber
}